// 
//

#include "stdafx.h"

#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <atlctrlw.h>

#include "resource.h"

#include "netfwView.h"
#include "aboutdlg.h"
#include "MainFrm.h"
#include "Windows.h"
#include <Atlstr.h>

CAppModule _Module;

CString GetIniString(CString key)
{
	TCHAR   szValue[MAX_PATH];
	TCHAR   path[MAX_PATH];   
	GetCurrentDirectory(MAX_PATH,path);
	_tcscat(path, _T("\\custom_action.ini"));
	::GetPrivateProfileString(_T("CUSTOM_INFO"), key, NULL, szValue, MAX_PATH-1, path);
	return szValue;
}

int Run(CString szInstallType, CString szInstallPath)
{  		
	TCHAR lpBuffer[MAX_PATH+1];
	UINT uSize (MAX_PATH+1);
 
	CString szCommandLine;
	CString szDefaultPath;

	DWORD dwExitCode;
	LPSTR pszCommandLine;

	GetWindowsDirectory(lpBuffer, uSize);

	CString szNgen = lpBuffer;
	szNgen.Append (GetIniString(_T("ngen")));

	CString szTwaingui;

	if (szInstallPath == "twaingui")
	{
		szTwaingui = lpBuffer;
		szTwaingui.Append (GetIniString(_T("twaingui")));
	}
	else
	{	
		szDefaultPath = szInstallPath;
		szTwaingui = szDefaultPath + GetIniString(_T("homecenter"));

//		szTwaingui.Append ("\\twain_32\\kodak\\kds_aio5000\\twaingui.exe");
	}	
	
	if (szInstallType == "INSTALL")
	{
		szCommandLine = szNgen + " install "  + "\"" + szTwaingui + "\"" + " /silent /nologo";
	}
	else if (szInstallType == "REMOVE")
	{
		szCommandLine = szNgen + " uninstall " + "\"" + szTwaingui + "\"" + " /silent /nologo";
	}
	pszCommandLine = szCommandLine.GetBuffer(MAX_PATH);
	szCommandLine.ReleaseBuffer();



	STARTUPINFO	si = {0};
	PROCESS_INFORMATION	pi = {0};

//	CREATE_NO_WINDOW
	BOOL bProcessCreated = CreateProcess(NULL, //szRealEXE,		// Who to run
										pszCommandLine,	// Commandline
										NULL,							// Process security
										NULL,							// Thread security
										FALSE,							// Do not inherit handles
										CREATE_NO_WINDOW,		// Allow us to wait for child prosses to complete
										NULL,							// Use default environment
										NULL,				// Use default starting directory
										&si,
										&pi);

	// Excellent, we kicked it off.  We need to wait for it to finish.									
	WaitForSingleObject(pi.hProcess, INFINITE);  //Waits for each individual job to complete
	
	GetExitCodeProcess(pi.hProcess, &dwExitCode);			
	
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return 1;
		
}


int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to 
// make the EXE free threaded. This means that calls come in on a random RPC thread.
//	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	ATLASSERT(SUCCEEDED(hRes));

	// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
	::DefWindowProc(NULL, 0, 0, 0L);

	AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);	// add flags to support other controls

	hRes = _Module.Init(NULL, hInstance);
	ATLASSERT(SUCCEEDED(hRes));
	int nRet (0);

	CString cmdArg = lpstrCmdLine;
	CString cmdArg1;
	CString cmdArg2;
		
	int nfind;
	int ntotal;
	
	nfind = cmdArg.Find (",");

	cmdArg1 = cmdArg.Left(nfind);

	ntotal = cmdArg.GetLength ();
	ntotal = (ntotal - nfind) - 1;
	cmdArg2 = cmdArg.Right(ntotal);

	if (cmdArg1 == "/INSTALL")
	{
		nRet = Run("INSTALL", cmdArg2);
	}
	else if (cmdArg1 == "/REMOVE")
	{
		nRet = Run("REMOVE", cmdArg2);
	}

	_Module.Term();
	::CoUninitialize();

	return nRet;
}
